<template>
  <div>
    <test t1="商品管理" t2="商品列表" path="/home">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入商品名称关键词"
            v-model="queryInfo.query"
            clearable
            @change="getGoodsList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddGood">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList" border style="width: 100%" stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="95px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="70px"></el-table-column>
        <el-table-column label="创建时间" width="140px">
          <template slot-scope="scope">{{scope.row.add_time | dateFormat }}</template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGood(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <!-- layout 组件布局，子组件名用逗号分隔,表示要在页面渲染中使用的组件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </test>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取商品列表的查询参数对象
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      // 商品列表
      goodsList: [],
      // 总数据条数
      total: 0
    };
  },
  methods: {
    // 获取商品列表
    async getGoodsList() {
      const { data: res } = await this.$axios.get("goods", {
        params: this.queryInfo
      });
      // console.log(res);
      if (res.meta.status != 200) this.$message.error("获取商品列表失败！");
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    // 每页显示条数变化时触发
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`);
      this.queryInfo.pagesize = val;
      this.getGoodsList();
    },
    // 当前页码发生变化时触发
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`);
      this.queryInfo.pagenum = val;
      this.getGoodsList();
    },
    // 删除商品
    async deleteGood(goodId) {
      const confirmResult = await this.$confirm(
        "您确定要永久删除该商品吗？",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (confirmResult == "cancel")
        return this.$message({ type: "info", message: "已取消删除!" });
      const { data: res } = await this.$axios.delete(`goods/${goodId}`);
      if (res.meta.status != 200) return this.$message.error("删除参数失败！");
      this.getGoodsList();
    },
    // 添加商品
    goAddGood() {
      // 编程式导航跳转添加商品路由
      // $router 表示全局路由对象
      // $route 表示当前页面的路由信息
      this.$router.push("goods/add");
    }
  },
  created() {
    this.getGoodsList();
  }
};
</script>

<style lang="less" scoped>
</style>